package com.clinic.action.drug;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.common.AuthenticationAction;
import com.clinic.entity.gui.GuiDrug;
import com.clinic.entity.gui.GuiDrugBrand;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.logic.drug.DrugBrandLogic;
import com.clinic.logic.drug.DrugLogic;
import com.clinic.util.CommonUtil;

public class DrugAction extends AuthenticationAction {

	private static final long serialVersionUID = -5026190848476237150L;
	
	@Autowired
	private DrugLogic drugLogic;
	@Autowired
	private DrugBrandLogic drugBrandLogic;
	
	private List<GuiDrugBrand> guiDrugBrands;
	private List<GuiDrug> guiDrugs;
	private GuiPagination guiPagination;
	private GuiDrug guiDrug;
	private long id;
	private String term;
	private SortProperty sort;
	private long size;
	private int page = 1;
	private int totalStockCount;

	public String registration() {
		if(isDoctorLogin() || isAssistantLogin()) {
			guiDrugBrands = drugBrandLogic.getDrugBrands(new Index(100));
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;		
	}
	
	public String show(){
		if (isDoctorLogin() || isAssistantLogin()) {
			guiDrug = drugLogic.getDrugDetailsById(id);
			totalStockCount = drugLogic.getDrugStockCount(id);
			guiDrug.setTotalStockCount(totalStockCount);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String edit(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiDrugBrands = drugBrandLogic.getDrugBrands(new Index(100));
			guiDrug = drugLogic.getDrugDetailsById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String delete(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiDrug = drugLogic.getDrugDetailsById(id);
			if(guiDrug.isAvailable()) {
				drugLogic.deleteDrugDetailsById(id);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}	
		return ActionResult.AUTH_FAILED;
	}
	
	public String insert() {
		if(isDoctorLogin() || isAssistantLogin()) {
			boolean isValidDrug = drugLogic.isValidDrugInsert(guiDrug);
			if(!isValidDrug) {
				addActionError("Failed to insert.This drug is available");
			}
			
			boolean isValidWeight = drugLogic.isValidWeight(guiDrug);
			if(!isValidWeight) {
				addActionError("Failed to insert.Invalid weight");
			}
			
			boolean isValidMinimum = drugLogic.isValidMinimum(guiDrug);
			if(!isValidMinimum) {
				addActionError("Failed to insert.Invalid minimum amount");
			}
			
			if(hasActionErrors()) {
				guiDrugBrands = drugBrandLogic.getDrugBrands(new Index());
				return ActionResult.ERROR;
			} else {
				guiDrug = drugLogic.insertDrug(guiDrug);  // catch guiDrug object details returned from insertDrug()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String update(){
		if(isDoctorLogin() || isAssistantLogin()) {
			boolean isValidDrugUpdate = drugLogic.isValidDrugUpdate(guiDrug);
			if(!isValidDrugUpdate) {
				addActionError("Failed to update.This drug is available");
			}
			
			boolean isValidWeight = drugLogic.isValidWeight(guiDrug);
			if(!isValidWeight) {
				addActionError("Failed to update.Invalid weight");
			}
			
			boolean isValidMinimum = drugLogic.isValidMinimum(guiDrug);
			if(!isValidMinimum) {
				addActionError("Failed to update.Invalid minimum amount");
			}
			
			if(hasActionErrors()) {
				guiDrugBrands = drugBrandLogic.getDrugBrands(new Index());
				return ActionResult.ERROR;
			} else {
				guiDrug = drugLogic.updateDrug(guiDrug);
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.ERROR;
	}
	
	public String list(){
		if(isDoctorLogin() || isAssistantLogin()) {
			Index index = CommonUtil.getIndexBypage(page);
			if (StringUtils.isNotBlank(term) || sort != null) {
				guiDrugs = drugLogic.getDrugListByTerm(term, sort.getColumn(), index);	
				guiPagination = drugLogic.getPaginationOfDrugsByTerm(term, sort.getColumn(), page);
				size = guiPagination.getTotalCount();
			} else {
				guiDrugs = drugLogic.getDrugList(index);				
				guiPagination = drugLogic.getPaginationOfDrugs(page);
				size = guiPagination.getTotalCount();
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;		
	}

	public List<GuiDrugBrand> getGuiDrugBrands() {
		return guiDrugBrands;
	}

	public void setGuiDrugBrands(List<GuiDrugBrand> guiDrugBrands) {
		this.guiDrugBrands = guiDrugBrands;
	}

	public GuiDrug getGuiDrug() {
		return guiDrug;
	}

	public void setGuiDrug(GuiDrug guiDrug) {
		this.guiDrug = guiDrug;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public boolean isTermExist() {
		return StringUtils.isNotBlank(term);
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}
	
	public boolean isSortExist() {
		return (sort != null);
	}

	public SortProperty getSort() {
		return sort;
	}
	
	public void setSort(String sortProperty) {
		this.sort = SortProperty.getValue(sortProperty);
	}

	public List<GuiDrug> getGuiDrugs() {
		return guiDrugs;
	}

	public void setGuiDrugs(List<GuiDrug> guiDrugs) {
		this.guiDrugs = guiDrugs;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}

	public int getTotalStockCount() {
		return totalStockCount;
	}

	public void setTotalStockCount(int totalStockCount) {
		this.totalStockCount = totalStockCount;
	}
	
}
